package com.cloudsigns.factorymanager2.produce.activity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.cloudsigns.factorymanager2.LoginActivity;
import com.cloudsigns.factorymanager2.MyApplicaton;
import com.cloudsigns.factorymanager2.R;
import com.cloudsigns.factorymanager2.cusenum.CommonEnum;
import com.cloudsigns.factorymanager2.engineering.bean.AccountInfoBean;
import com.cloudsigns.factorymanager2.engineering.fragment.BaseFragmentActivity;
import com.cloudsigns.factorymanager2.produce.activity.produce_team.ProduceTeamMainActivity;
import com.cloudsigns.factorymanager2.produce.fragment.ProduceManagerFragment;
import com.cloudsigns.factorymanager2.produce.fragment.ProduceOrderFragment;
import com.cloudsigns.factorymanager2.project.adapter.ProjectMainViewPageAdapter;
import com.cloudsigns.factorymanager2.project.customview.CustomViewPager;
import com.cloudsigns.factorymanager2.project.customview.DropDownPoupWindow;
import com.cloudsigns.factorymanager2.project.customview.GlideCircleTransform;
import com.cloudsigns.factorymanager2.project.customview.PageIndicatorView;
import com.cloudsigns.factorymanager2.project.fragment.InternalOfficeFragment;
import com.cloudsigns.factorymanager2.project.fragment.SettingFragmet;
import com.cloudsigns.factorymanager2.project.utils.ConstantUtil;

import java.util.ArrayList;

/**
 * 生产
 */
public class ProduceMainActivity extends BaseFragmentActivity implements View.OnClickListener {
    private String TAG = ProduceMainActivity.class.getSimpleName();
    private RelativeLayout rl_back;
    private TextView tv_title;
    private TextView tv_headtop, tv_headbottom;
    private ImageView img_head;
    private CustomViewPager viewpager;
    private LinearLayout ll_demand_center;
    private ArrayList<Fragment> listFragments;
    private ProjectMainViewPageAdapter adapter;
    private RelativeLayout layout_produce, layout_order, layout_office, layout_systems;
    private ImageView img_produce, img_order, img_office, img_systems;
    private Fragment produceManagerFragment, produceOrderFragment, internalOffficeFragemnt, systemFragment;
    private PageIndicatorView indicator;
    private DropDownPoupWindow popupWindow;
    private View title_layout;
    public static final int PRODUCE = 0;
    public static final int ORDER = 1;
    public static final int OFFICE = 2;
    public static final int SYSTEM = 3;
    public int currentIndex = PRODUCE;
    private AccountInfoBean accountInfoBean;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);//去掉标题栏
        // 沉浸式状态栏
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Window window = getWindow();
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(Color.TRANSPARENT);
            // window.setNavigationBarColor(Color.TRANSPARENT);
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        }
        setContentView(R.layout.layout_produce_main);

        init();
        viewpager.setCurrentItem(PRODUCE);
        tv_title.setText("");
        ll_demand_center.setVisibility(View.VISIBLE);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        if (intent != null) {
            boolean isExit = intent.getBooleanExtra(ConstantUtil.KEY_1, false);
            if (isExit) {
                Intent mItent = new Intent(this, LoginActivity.class);
                startActivity(mItent);
                this.finish();
            }
        }
    }

    private void init() {
        rl_back = findViewById(R.id.rl_back);
        tv_title = findViewById(R.id.tv_title);
        tv_headtop = findViewById(R.id.tv_headtop);
        tv_headbottom = findViewById(R.id.tv_headbottom);
        img_head = findViewById(R.id.img_head);
        title_layout = findViewById(R.id.title_layout);
        ll_demand_center=findViewById(R.id.ll_demand_center);
        layout_produce = findViewById(R.id.layout_produce);
        layout_order = findViewById(R.id.layout_order);
        layout_office = findViewById(R.id.layout_office);
        layout_systems = findViewById(R.id.layout_systems);
        img_produce = findViewById(R.id.img_produce);
        img_order = findViewById(R.id.img_order);
        img_office = findViewById(R.id.img_office);
        img_systems = findViewById(R.id.img_systems);
        viewpager = findViewById(R.id.viewpager);
        viewpager.setScanScroll(false);
        indicator = findViewById(R.id.indicator);

        rl_back.setVisibility(View.GONE);
        img_head.setOnClickListener(this);
        layout_produce.setOnClickListener(this);
        layout_order.setOnClickListener(this);
        layout_office.setOnClickListener(this);
        layout_systems.setOnClickListener(this);

        produceManagerFragment = new ProduceManagerFragment();
        produceOrderFragment = new ProduceOrderFragment();
        internalOffficeFragemnt = new InternalOfficeFragment();
        systemFragment = new SettingFragmet();


        listFragments = new ArrayList<Fragment>();
        listFragments.add(produceManagerFragment);
        listFragments.add(produceOrderFragment);
        listFragments.add(internalOffficeFragemnt);
        listFragments.add(systemFragment);

        adapter = new ProjectMainViewPageAdapter(getSupportFragmentManager(), listFragments);
        viewpager.setAdapter(adapter);
        viewpager.setOffscreenPageLimit(4);

        indicator.setViewPager(viewpager);
        indicator.setFades(false);
        indicator.setSelectedColor(getResources().getColor(R.color.rgb_17a5ef));
        viewpager.setCurrentItem(PRODUCE);
        showSelectedOption(PRODUCE);

        indicator.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

            @Override
            public void onPageSelected(int position) {
                showSelectedOption(position);
                currentIndex = position;
            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {
            }

            @Override
            public void onPageScrollStateChanged(int arg0) {

            }
        });


        accountInfoBean = MyApplicaton.getApplication().getAccountInfoBean();
        if (null != accountInfoBean) {
            if (accountInfoBean.getRoleCategory() == CommonEnum.ROLECATEGORY.MANAGER) {
                tv_headbottom.setText("生产 / " + accountInfoBean.getAccountName());
                String companyName = accountInfoBean.getCompanyName();
                if (!TextUtils.isEmpty(companyName) && !"null".equals(companyName)) {
                    tv_headtop.setText(companyName);
                }
                Glide.with(this).load(accountInfoBean.getAvatar()).bitmapTransform(new GlideCircleTransform(this)).crossFade(1000).error(R.mipmap.default_avatar).into(img_head);
            }
        }

    }

    private void showSelectedOption(int pos) {
        // TODO Auto-generated method stub
        switch (pos) {
            //　生产
            case PRODUCE:
                layout_produce.setSelected(true);
                layout_produce.setBackgroundColor(getResources().getColor(R.color.rgb_17a5ef));
                img_produce.setImageResource(R.mipmap.tab_produce_pressed);

                layout_order.setSelected(false);
                layout_order.setBackgroundColor(getResources().getColor(R.color.rgb_f2f2f2));
                img_order.setImageResource(R.mipmap.tab_project_order);

                layout_office.setSelected(false);
                layout_office.setBackgroundColor(getResources().getColor(R.color.rgb_f2f2f2));
                img_office.setImageResource(R.mipmap.tab_project_office);

                layout_systems.setSelected(false);
                layout_systems.setBackgroundColor(getResources().getColor(R.color.rgb_f2f2f2));
                img_systems.setImageResource(R.mipmap.tab_project_systems);
                break;
            // 工单
            case ORDER:
                layout_produce.setSelected(false);
                layout_produce.setBackgroundColor(getResources().getColor(R.color.rgb_f2f2f2));
                img_produce.setImageResource(R.mipmap.tab_produce_normal);

                layout_order.setSelected(true);
                layout_order.setBackgroundColor(getResources().getColor(R.color.rgb_17a5ef));
                img_order.setImageResource(R.mipmap.tab_project_order_pressed);

                layout_office.setSelected(false);
                layout_office.setBackgroundColor(getResources().getColor(R.color.rgb_f2f2f2));
                img_office.setImageResource(R.mipmap.tab_project_office);

                layout_systems.setSelected(false);
                layout_systems.setBackgroundColor(getResources().getColor(R.color.rgb_f2f2f2));
                img_systems.setImageResource(R.mipmap.tab_project_systems);
                break;
            // 办公
            case OFFICE:
                layout_produce.setSelected(false);
                layout_produce.setBackgroundColor(getResources().getColor(R.color.rgb_f2f2f2));
                img_produce.setImageResource(R.mipmap.tab_produce_normal);

                layout_order.setSelected(false);
                layout_order.setBackgroundColor(getResources().getColor(R.color.rgb_f2f2f2));
                img_order.setImageResource(R.mipmap.tab_project_order);

                layout_office.setSelected(true);
                layout_office.setBackgroundColor(getResources().getColor(R.color.rgb_17a5ef));
                img_office.setImageResource(R.mipmap.tab_project_office_pressed);

                layout_systems.setSelected(false);
                layout_systems.setBackgroundColor(getResources().getColor(R.color.rgb_f2f2f2));
                img_systems.setImageResource(R.mipmap.tab_project_systems);
                break;
            // 系统
            case SYSTEM:
                layout_produce.setSelected(false);
                layout_produce.setBackgroundColor(getResources().getColor(R.color.rgb_f2f2f2));
                img_produce.setImageResource(R.mipmap.tab_produce_normal);

                layout_order.setSelected(false);
                layout_order.setBackgroundColor(getResources().getColor(R.color.rgb_f2f2f2));
                img_order.setImageResource(R.mipmap.tab_project_order);

                layout_office.setSelected(false);
                layout_office.setBackgroundColor(getResources().getColor(R.color.rgb_f2f2f2));
                img_office.setImageResource(R.mipmap.tab_project_office);

                layout_systems.setSelected(true);
                layout_systems.setBackgroundColor(getResources().getColor(R.color.rgb_17a5ef));
                img_systems.setImageResource(R.mipmap.tab_project_systems_pressed);
                break;
        }
    }

    @Override
    public void onClick(View view) {
        Intent mIntent;
        switch (view.getId()) {
            // 头像
            case R.id.img_head:
                Intent intent = new Intent(ProduceMainActivity.this, AvatarImageShowerActivity.class);
                intent.putExtra("imageUrl", accountInfoBean.getAvatar());
                startActivity(intent);
                break;
            //  生产
            case R.id.layout_produce:
                if (currentIndex != PRODUCE) {
                    currentIndex = PRODUCE;
                    viewpager.setCurrentItem(PRODUCE);
                    showSelectedOption(PRODUCE);
                }
                break;
            // 工单
            case R.id.layout_order:
                if (currentIndex != ORDER) {
                    currentIndex = ORDER;
                    viewpager.setCurrentItem(ORDER);
                    showSelectedOption(ORDER);
                }
                break;
            // 办公
            case R.id.layout_office:
                if (currentIndex != OFFICE) {
                    currentIndex = OFFICE;
                    viewpager.setCurrentItem(OFFICE);
                    showSelectedOption(OFFICE);
                }
                break;
            // 系统
            case R.id.layout_systems:
                if (currentIndex != SYSTEM) {
                    currentIndex = SYSTEM;
                    viewpager.setCurrentItem(SYSTEM);
                    showSelectedOption(SYSTEM);
                }
                break;
        }
    }
}
